package com.seer.auto.dao;

import com.seer.auto.error.DataAccessNotFoundException;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

@Service("MyJdbcTemplate")
public class MyJdbcTemplate extends JdbcTemplate {

    @Autowired
    public MyJdbcTemplate(DataSource ds) {
        super(ds);
    }

    @Override
    public <T> T queryForObject(String sql, RowMapper<T> rowMapper, Object... args) throws DataAccessException {
        List<T> results = query(sql, rowMapper, args);
        if (results.isEmpty()) {
            throw new DataAccessNotFoundException("Incorrect result size: expected 1, actual 0");
        }
        return results.get(0);
    }


}
